#! /bin/sh

set -e
set -u

if [ $# -ne 2 ] ; then
    echo "Usage: $0 <shader.list> <file.frag>" >&2
    exit 1
fi

SHADER_LIST="$1"
FRAGPATH="$2"
FRAGFILE="$(basename "${FRAGPATH}")"
FRAGSTR="${FRAGFILE%.frag}"
FRAG_ID="${FRAGSTR%.*}"
FRAG_REV="${FRAGSTR#*.}"
FRAG_NICK="$(grep -E "^${FRAGSTR}[[:blank:]]" "${SHADER_LIST}" | sed -E 's/^[0-9]+\.[0-9]+[[:blank:]]+//')"

if [ -z "${FRAG_NICK}" ] ; then
    FRAG_NICK="${FRAGSTR}"
fi

echo "  {"
echo "    ${FRAG_ID}, ${FRAG_REV}, \"${FRAG_NICK}\","

sed -E \
    -e 's/[[:blank:]]+$//' \
    -e 's/\\/\\\\/g' \
    -e 's/"/\\"/g' \
    -e 's/^/"/' \
    -e 's/$/\\n"/' \
    "${FRAGPATH}"

echo "  },"
